//
//  MyCommentViewController.m
//  suRead
//
//  Created by 李 诗洋 on 15/11/19.
//  Copyright © 2015年 bao. All rights reserved.
//

#import "MyCommentViewController.h"
#import "Masonry.h"
#import "UIControl+Blocks.h"
#import "ServiceNews.h"
#import "AccountManager.h"
#import "UIViewController+HUD.h"
#import "MyCommentCell.h"
#import "StationMessageCell.h"
#import "M_MyComment.h"
#import "M_Letter.h"

@interface MyCommentViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)UIView *topBarView;

/**
 *  @brief 我的评论数组
 */
@property (nonatomic,strong)NSArray *commentArray;
/**
 *  @brief 站内私信数组
 */
@property (nonatomic,strong)NSArray *letterArray;

@property (nonatomic,strong)UITableView *newsCommentTableView;
@property (nonatomic,strong)UITableView *CommentTableView;
@property (nonatomic,strong)UISegmentedControl *myCommentSegmented;
@end

@implementation MyCommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.commentArray = [NSArray new];
    self.letterArray  = [NSArray new];
    
    __weak typeof(self) weak_self = self;
    self.view.backgroundColor = [UIColor whiteColor];
    self.topBarView = [UIView new];
    self.topBarView.backgroundColor = [UIColor colorWithRed:248/255.0f green:248/255.0f blue:248/255.0f alpha:1.0f];
    [self.view addSubview:self.topBarView];
    [self.topBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(weak_self.topBarView.superview);
        make.height.equalTo(@64.0f);
    }];
    
    UIButton *buttonBack = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonBack setImage:[UIImage imageNamed:@"button_back"] forState:UIControlStateNormal];
    [buttonBack addActionForControlEvents:UIControlEventTouchUpInside usingBlock:^(UIControl *sender, UIEvent *event) {
        //返回
        [weak_self.navigationController popViewControllerAnimated:YES];
    }];
    [weak_self.topBarView addSubview:buttonBack];
    
    [buttonBack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(weak_self.topBarView);
        make.size.mas_equalTo(CGSizeMake(44.0f, 44.0f));
    }];
    UILabel *titleLabel = [UILabel new];
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.text = @"我的评论";
    titleLabel.font = [UIFont fontWithName:@"Helvetica" size:17.f];
    [self.topBarView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weak_self.topBarView);
        make.bottom.equalTo(weak_self.topBarView.mas_bottom).with.offset(-10.0f);
    }];
    
    self.newsCommentTableView = [UITableView new];
    self.newsCommentTableView.delegate = self;
    self.newsCommentTableView.dataSource = self;
    self.newsCommentTableView.hidden = NO;
    self.newsCommentTableView.tableFooterView = [UIView new];
    [self.view addSubview:self.newsCommentTableView];
    [self.newsCommentTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weak_self.topBarView.mas_bottom);
        make.left.equalTo(weak_self.newsCommentTableView.superview.mas_left);
        make.right.equalTo(weak_self.newsCommentTableView.superview.mas_right);
        make.bottom.equalTo(weak_self.newsCommentTableView.superview.mas_bottom);
    }];
    self.newsCommentTableView.separatorStyle = UITableViewCellSelectionStyleNone;

    [self.newsCommentTableView registerNib:[UINib nibWithNibName:@"MyCommentCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"MyCommentCell"];
    
    [self showHUDProgress:@""];
    [ServiceNews doFetchMyComments:[AccountManager sharedAccountManager].f_uid
                           success:^(id responseObject, NSString *message)
    {
        [self removeHUDProgress];
        self.commentArray = responseObject;
        if (self.commentArray.count > 0) {
            [self.newsCommentTableView reloadData];
        } else {
            [self showHUDText:@"暂无评论"];
        }
    }
                           failure:^(NSString *message)
    {
        [self removeHUDProgress];
        [self showHUDText:message];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.commentArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 138.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    M_MyComment *commentData = [self.commentArray objectAtIndex:indexPath.row];
    return [MyCommentCell heightWithContent:commentData.f_content];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyCommentCell *cell = (MyCommentCell *)[tableView dequeueReusableCellWithIdentifier:@"MyCommentCell"];
    M_MyComment *commentData = [self.commentArray objectAtIndex:indexPath.row];
    [cell setCellData:commentData];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
